package com.summer.shopping.chat.handler;

import com.summer.shopping.chat.UserChatPool;
import com.summer.shopping.chat.WSInfo;
import com.summer.shopping.domain.table.Chat;
import com.summer.shopping.enums.chat.ChatInfoTypeConstant;
import com.summer.shopping.service.ChatService;
import com.summer.shopping.utils.WSUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.time.LocalDateTime;

@Component
public class ChatInfoHandler implements WSInfoTypeHandler {
    @Autowired
    UserChatPool userChatPool;


    @SneakyThrows
    @Override
    public Object handleInfo(WSInfo.Standard info) {
        //发回信息
        Long from = info.getFrom();
        Long to = info.getTo();
        WebSocketSession receiverSession = userChatPool.getSession(to);
        if (receiverSession == null) {
            return null;
        }
        info.setFrom(to);
        info.setTo(from);
        receiverSession.sendMessage(WSUtils.toTextMessage(info));
        return null;
    }

    @Override
    public boolean isSupport(String type) {
        return type.equals(ChatInfoTypeConstant.INFO);
    }
}
